/**
 * 
 */
package com.kunbo.cn.service.impl.core.inventory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.kunbo.cn.dao.core.inventory.InventoryDao;
import com.kunbo.cn.entity.core.inventory.Inventory;
import com.kunbo.cn.service.core.inventory.InventoryService;

/**
 *
 * @author bjl  
 * @date 2019年2月26日 
 */
@Service
public class InventoryServiceImpl  implements InventoryService{
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
	@Autowired
	private InventoryDao dao;
	@Autowired
    private JdbcTemplate jdbcTemplate;
	@Override
	public void save(Inventory inventory) {
		inventory.setCurentDate(sdf1.format(new Date()));
		inventory.setCreate_time(sdf.format(new Date()));
		dao.save(inventory);
		
	}
	@Override
	public void update(Inventory inventory) {
		dao.save(inventory);
	}
	
	@Override
	public void delete(String id) {
		dao.delete(id);
	}
	
	@Override
	public Inventory findById(String id) {
		return dao.findOne(id);
	}
	
	/*@Override
	public void updateSate(OrderCarryOver carryOver) {
		dao.save(carryOver);
	}*/
	/**
	 * 分页条件列表查询
	 */
	@Override
	public Page<Inventory> findInventory(String materType, String curentDate,Integer pageNo, Integer pageSize) {
		Pageable pageable = new PageRequest(pageNo-1, pageSize);
        Specification<Inventory> confusion=new Specification<Inventory>() {
            @Override
            public Predicate toPredicate(Root<Inventory> root, CriteriaQuery<?> query,
                    CriteriaBuilder criteriaBuilder) {
            	List<Predicate> predicates = new ArrayList<>();
            	if(!"".equals(materType)) {
            		predicates.add(criteriaBuilder.like(root.get("materType"), "%"+materType+"%"));
            	}
            	if(!"".equals(curentDate)) {
            		predicates.add(criteriaBuilder.like(root.get("curentDate"), "%"+curentDate+"%"));
            	}
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return dao.findAll(confusion, pageable);
	}
	@Override
	public List<Map<String, Object>> inventoryCount() {
		String sql = "select sum(inventory) as `sum`,mater_type from business_inventory_manage group by mater_type";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> map : list) {
			String type = "select id,name from business_matter_type_manage where name ='"  + map.get("mater_type").toString() +"'";
			//String type = "select id,name from business_matter_type_manage where id ="  + Integer.parseInt(map.get("mater_type").toString());
			//String type = "select id,name from business_matter_type_manage where id ="  + Integer.parseInt(map.get("mater_type").toString());
			//String type = "select id,name from business_matter_type_manage where mater_type =" + String.valueOf(map.get("mater_type").toString());
			List<Map<String, Object>> typeList = jdbcTemplate.queryForList(type);
			if(typeList != null && typeList.size() > 0) {
				map.put("mater_type", typeList.get(0).get("name").toString());
				map.put("id", Integer.parseInt(typeList.get(0).get("id").toString()));
			}
		}
		return list;
	}
	@Override
	public List<Map<String, Object>> showMaterTypeMes(String code, String materialType, Integer page,
			Integer pageSize) {
		String sql = "select * from business_inventory_manage where 1 = 1";
		if(code != null && !code.equals("")) {
			sql += " and inventory_code like '%" + code + "%'";
		}
		if(materialType != null && !materialType.equals("")) {
			sql += " and mater_type = '" + materialType + "'" ;
		}
		sql += " order by id desc limit " + (page * pageSize) + "," + pageSize;
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> map : list) {
			String mater_type_sql = "select name from business_matter_type_manage where name = '" + map.get("mater_type").toString() +"'";
			//String mater_type_sql = "select name from business_matter_type_manage where id = " + Integer.parseInt(map.get("mater_type").toString()) ;
			//String mater_type_sql = "select name from business_matter_type_manage where id = " +  String.valueOf(map.get("mater_type").toString()) ;
			if(jdbcTemplate.queryForList(mater_type_sql) != null && jdbcTemplate.queryForList(mater_type_sql).size() > 0) {
				map.put("mater_type", jdbcTemplate.queryForList(mater_type_sql).get(0).get("name"));
			}
		}
		return list;
	}
	@Override
	public List<Map<String, Object>> list(String material, Integer pageNo,
			Integer pageSize) {
		String sql = "select * from business_inventory_manage where 1 = 1";
		if(material != null && !material.equals("")) {
			sql += " and inventory_code like '%" + material + "%'";
		}
		/*if(materialType != null && !materialType.equals("")) {
			sql += " and mater_type = '" + materialType + "'" ;
		}*/
		sql += " order by id desc limit " + (pageNo * pageSize) + "," + pageSize;
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> map : list) {
			//String mater_type_sql = "select name from business_matter_type_manage where name = '" + map.get("mater_type").toString() +"'";
			//String mater_type_sql = "select name from business_matter_type_manage where id = " + Integer.parseInt(map.get("mater_type").toString()) ;
			String material_sql = "select name from business_materials_manage where id = '" +Integer.parseInt( map.get("material").toString()) +"'";
			String wellhead_sql = "select text from select_wellhead where id = '" +Integer.parseInt( map.get("wellhead").toString()) +"'";//String mater_type_sql = "select name from business_matter_type_manage where id = " +  String.valueOf(map.get("mater_type").toString()) ;
			if(jdbcTemplate.queryForList(material_sql) != null && jdbcTemplate.queryForList(material_sql).size() > 0) {
				map.put("material", jdbcTemplate.queryForList(material_sql).get(0).get("name"));
			}
			if(jdbcTemplate.queryForList(wellhead_sql) != null && jdbcTemplate.queryForList(wellhead_sql).size() > 0) {
				map.put("wellhead", jdbcTemplate.queryForList(wellhead_sql).get(0).get("text"));
			}
		}
		return list;
	}
	
	@Override
	public List<Inventory> findInventorys(String material, Integer pageNo, Integer pageSize) {
		return null;
	}



}
